package com.example.newdemo.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;

public class HttpGetUtils {

    /**
     * http get请求, 不带参数
     * @param requestURL
     * @return
     */
    public static String doGetNoParameters(String requestURL, String proxyHost, Integer proxyPort) {
        // 记录信息
        StringBuffer buffer = new StringBuffer();

        HttpURLConnection conn = null;
        try {
            URL url = new URL(requestURL);
            // 判断是否需要代理模式请求http
            if (proxyHost != null && proxyPort != null) {
                // 如果是本机自己测试, 不需要代理请求,但发到服务器上的时候需要代理请求
                // 对http开启全局代理
                //System.setProperty("http.proxyHost", proxyHost);
                //System.setProperty("http.proxyPort", proxyPort);
                // 对https开启全局代理
                //System.setProperty("https.proxyHost", proxyHost);
                //System.setProperty("https.proxyPort", proxyPort);

                // 代理访问http请求
                Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
                conn = (HttpURLConnection) url.openConnection(proxy);
            } else {
                // 原生访问http请求，未代理请求
                conn = (HttpURLConnection) url.openConnection();
            }

            // 设置请求的属性
            conn.setDoOutput(true);                             // 是否可以输出
            conn.setRequestMethod("GET");                       // 请求方式, 只包含"GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE", "TRACE"六种
            conn.setConnectTimeout(60000);                      // 最高超时时间
            conn.setReadTimeout(60000);                         // 最高读取时间
            conn.setConnectTimeout(60000);                      // 最高连接时间


            conn.setRequestProperty("Content-type", "application/json");


            // 读取数据
            InputStream is = null;
            InputStreamReader inputReader = null;
            BufferedReader reader = null;
            try {
                // 调用HttpURLConnection连接对象的getInputStream()函数,
                // 将内存缓冲区中封装好的完整的HTTP请求电文发送到服务端。
                is = conn.getInputStream();                     // <===注意，实际发送请求的代码段就在这里

                int responseCode=conn.getResponseCode();

                inputReader = new InputStreamReader(is, "UTF-8");
                reader = new BufferedReader(inputReader);

                String temp;
                while ((temp = reader.readLine()) != null) {
                    buffer.append(temp);
                }
            } catch (Exception e) {
                System.out.println("HttpGetUtils doGetNoParameters error: " + e);
            } finally {
                try {
                    if (reader != null) {
                        reader.close();
                    }
                    if (inputReader != null) {
                        inputReader.close();
                    }
                    if (is != null) {
                        is.close();
                    }
                } catch (IOException e) {
                    System.out.println("HttpGetUtils doGetNoParameters error: " + e);
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 当http连接空闲时, 释放资源
            if (conn != null) {
                conn.disconnect();
            }
        }
        // 返回信息
        return buffer.length()==0 ? "" : buffer.toString();
    }


}
